package com.zhiyou100.controller;

import com.zhiyou100.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * action 方法的参数和返回值
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-19 22:20
 */
@RestController
@RequestMapping("/test02")
public class Test02 {
    /**
     * 单值类型的请求参数：接受同名的请求参数
     *
     * @param age  int 年龄
     * @param name string 姓名
     * @return json 串
     */
    @RequestMapping("/m1.action")
    @ResponseBody
    public String test01Method(Integer age, String name) {
        return name + ":::::111+" + age;
    }


    /**
     * 对象类型的请求参数：拿对象的属性来接受同名的请求参数
     *
     * @param s User 对象
     * @return s.toString();
     */
    @RequestMapping("/m2.action")
    @ResponseBody
    public String test02Method(User s) {
        return s.toString();
    }

    /**
     * HttpServletRequest / HttpServletResponse
     *
     * @param req  HttpServletRequest
     * @param resp HttpServletResponse
     * @throws Exception 抛出异常
     */
    @RequestMapping("/m4.action")
    public void test04Method(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println(req.getParameter("name") + "::::" + req.getParameter("age"));
        resp.getWriter().print("success");
    }


    /**
     * model: 作为 request 的域对象
     * httpsession
     *
     * @param session HttpSession
     * @param resp    HttpServletResponse
     * @throws Exception 异常
     */
    @RequestMapping("/m5.action")
    public void test05Method(HttpSession session, HttpServletResponse resp) throws Exception {
        resp.getWriter().print("success" + session.getId());
    }
}
